﻿// ********************************************************************
//  文件名: MySocket_Udp.cs
//  描述:
//  作者: 胡宇辰
//  创建时间: 2024-3-30
// 
//  修改历史:
// ********************************************************************

using System.Net;
using System.Net.Sockets;

namespace MyApp;

public class Hyu
{
    public int i;
}
public class MySocket_Udp
{
    static Socket serverSocket;
    static byte[] buffer = new byte[1024];
    public static void Start()
    {
        serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        try
        {
            serverSocket.Bind(new IPEndPoint(IPAddress.Any, 6324));
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        Console.WriteLine("Server started");
        EndPoint tempRemoteEP = new IPEndPoint(IPAddress.Any, 0);
        while (true)
        {
            int len = serverSocket.ReceiveFrom(buffer, ref tempRemoteEP);
            IPEndPoint remoteEP = (IPEndPoint) tempRemoteEP;
            serverSocket.SendTo(buffer, 0, len, SocketFlags.None, remoteEP);
        }
    }
}